package weektwo_day03_parser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Scanner;

import weektwo_day03_utils.Format;

public class DataProcess {
	private String[] paramsStrings;
	BufferedReader reader;
	// paramsStrings: 0->scp;1->inputPath;2->ip;3->outputPath
	public DataProcess(String[] paramsStrings) {
		super();
		this.paramsStrings = paramsStrings;
	}
	Scanner scanner = new Scanner(System.in);

	public void upLoad(InputStream in,OutputStream out) throws Exception{
		String command ="";
		String tip = null;
		FileInputStream fileInputStream;
		reader = new BufferedReader(new InputStreamReader(in));
		File file = new File(paramsStrings[1]);
		while(!file.exists()){
			System.out.println("file not find !please select again");
			command = scanner.nextLine();
			file = new File(Format.formatString(command)[1]);
		}		
		if(!"200sucess".equals(tip = reader.readLine())){
			System.out.println(tip);
			System.out.println(paramsStrings[3]);
			out.write(paramsStrings[3].getBytes());
		}
		String read = reader.readLine();
		System.out.println(read);
		fileInputStream = new FileInputStream(file);
		byte[] b = new byte[fileInputStream.available()];
		int len;
		while((len = fileInputStream.read(b))!=-1){
			out.write(b,0,len);
		}
		reader.close();
		fileInputStream.close();
		in.close();
		out.close();
	}
}
